package com.jagochan.mall.controller;

import com.jagochan.mall.common.result.ApiResult;
import com.jagochan.mall.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Api(tags = "minio文件管理")
@RestController
@RequestMapping("/minio")
public class MinioController {

    @Resource
    public MinioService minioService;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public ApiResult<String> upload(MultipartFile file) {
        String upload = minioService.upload(file);
        return ApiResult.success(upload);
    }

    @ApiOperation("预览图片")
    @GetMapping("/preview")
    public ApiResult<String> preview(String fileName) {
        String preview = minioService.preview(fileName);
        return ApiResult.success(preview);
    }

    @ApiOperation("删除文件")
    @GetMapping("/remove")
    public ApiResult<Boolean> remove(String fileName) {
        return ApiResult.success(minioService.remove(fileName));
    }

    /**
     * @param bucketName bucket名称
     * @param objectName 文件或文件夹名称
     */
    @ApiOperation("删除文件夹及文件")
    @GetMapping("/deleteObjects")
    public ApiResult<Boolean> deleteObjects(String bucketName, String objectName) {
        return ApiResult.success(minioService.deleteObjects(bucketName, objectName));
    }

}

